<?php


namespace App\Admin\Controllers;


use App\Admin\Actions\ProductBatch\AddArea;
use App\Admin\Actions\ProductBatch\AddSite;
use App\Models\ProductModel;
use App\Models\ProductsBatchModel;
use App\Models\WarehouseAreaModel;
use App\Models\WarehouseModel;
use App\Models\WarehouseShelfModel;
use App\Models\WarehouseSiteModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Table;
use Illuminate\Support\Collection;

class WarehouseShelfController extends AdminController
{
    use HasResourceActions;

    protected $title = '货架管理';

    public function index(Content $content)
    {
        return $content
            ->header('货架管理')
            ->description(trans('admin.list'))
            ->body($this->grid()->render());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new WarehouseShelfModel());

        $grid->column('shelf_id', '货架ID');
        $grid->column('shelf_name', '货架名')->editable();
        $grid->column('remark', '备注')->editable();
        $grid->column('warehouse_detail', '所属仓库')->display(function () {
            $name1 = $this->WarehouseArea->warehouse->warehouse_name;
            $name2 = $this->WarehouseArea->area_name;
            return $name1 . '--区域：' . $name2;
        });

        $grid->column('total_sites', '总货位')->display(function () {
            return $this->WarehouseSite()->count();
        })->modal('货位', function ($model) {
            $shelf = $model->WarehouseSite()->take(100)->get()->map(function (WarehouseSiteModel $site) {
                $product=$site->load(['ProductBatch.Product'])->toArray();
                $site['is_using'] = count($product['product_batch'])>0 ? '<b style="color: red">使用中</b>' : '<b style="color: green">空闲</b>';
                $site['product_name']= collect((array) $product['product_batch'])->map(function ($v) {
                    return "<span class='label label-success'>{$v['product']['product_name']}[{$v['stock']}".ProductsBatchModel::MAP_UNIT[$v['unit']]."]</span><span class='label label-default'>溯源码:".$v['source_code']."</span>";
                })->implode('&nbsp;<br/>');
                return $site->only(['site_id', 'site_name', 'remark', 'is_using', 'product_name']);
            });
            return new Table(['ID', '货位名', '备注', '是否使用中', '存放产品'], $shelf->toArray());
        });

        $grid->column('sites', '可用货位')->display(function () {
            return $this->WarehouseSite()->has('ProductBatch','=',0)->count();
        })->modal('货位', function ($model) {
            $site = $model->WarehouseSite()->has('ProductBatch','=',0)->take(100)->get()->map(function (WarehouseSiteModel $site) {
                $product=$site->load(['ProductBatch']);
                $site['is_using'] = $product->ProductBatch->count()>0 ? '<b style="color: red">使用中</b>' : '<b style="color: green">空闲</b>';
                return $site->only(['site_id', 'site_name', 'remark', 'is_using']);
            });

            return new Table(['ID', '货位名', '备注', '是否使用中'], $site->toArray());
        });

        $grid->column('created_at', '创建时间');

        $grid->actions(function ($action) {
            $action->disableView();
            $action->add(new AddSite());
        });
        return $grid;
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WarehouseShelfModel());
        $form->select('warehouse_area_id', '所属区域')->options(function () {
            $data = WarehouseAreaModel::with(['warehouse'])->get();
            $options = [];
            foreach ($data as $k => $v) {
                $options[$v['warehouse_area_id']] = '' . $v['warehouse']['warehouse_name'] . '  ===>  ' . $v['area_name'];
            }
            return $options;
        })->required();

        $form->text('shelf_name', '货架名')->required();
        $form->textarea('remark', '备注');

        $form->disableCreatingCheck();
        $form->disableEditingCheck();
        $form->disableViewCheck();
        $form->display('created_at', 'Created At');
        $form->display('updated_at', 'Updated At');

        return $form;
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('货架管理')
            ->description(trans('admin . create'))
            ->body($this->form());
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('货架管理')
            ->description(trans('admin . edit'))
            ->body($this->form()->edit($id));
    }
}
